import 'package:bot_toast/bot_toast.dart';
import 'package:niuniu_lulu/index/index.dart';
import 'dart:math';

import '../../home/index.dart';

enum GroupType{
  GroupOne,GroupTwo,GroupThree,GroupFour,GroupFive,Group6,Group7
}

class PageValue extends ValueNotifier<int>{
  PageValue(int value) : super(value);
}
class GroupsWidget extends StatefulWidget{
  final GroupType type;
  GroupsWidget({this.type});

  @override
  _GroupsWidgetState createState() => _GroupsWidgetState();

}

class _GroupsWidgetState extends State<GroupsWidget>{
  ScrollController _controller = ScrollController();
  PageValue page;
  double listHight = 600;
  List<List<Widget>> dataSource = [];

  @override
  void initState() {
    page = PageValue(0);
    super.initState();
  }

  @override
  void didUpdateWidget(GroupsWidget oldWidget) {
    page = PageValue(0);
    _controller.animateTo(0, duration: Duration(microseconds: 300), curve: Curves.ease);
    page.addListener(() {
      _controller.animateTo(listHight.h * page.value, duration: Duration(microseconds: 300), curve: Curves.ease);
    });
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    page.value = 0;
    page.dispose();
    super.dispose();
  }

  List<String> lTitles = ["上六","上五","上四","上三","上二","上一"];
  List<String> rTitles = ["下六","下五","下四","下三","下二","下一"];

  List<Widget> computeResultByType({double highVal,double lowVal,double cVal, double dVal,double ddVal,int index}){
    List<Widget> widgets = [];
    switch(widget.type) {
      case GroupType.GroupOne:
        if(cVal == 0){ break; }
        if (dVal > 0){
          if (index == 0){
            var res1 = formulaOne(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
            var res2 = formulaOne2(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal,ddVal: ddVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "当前值",backgroundColor: Colors.redAccent,titleColor: Colors.white));
            widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "当前值",backgroundColor: Colors.blue,titleColor: Colors.white));
          }else{
            var res1 = formulaOne(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index).toDouble()));
            var res2 = formulaOne2(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index).toDouble()),ddVal: ddVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index).toString()));
            widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: dVal > 0?index.toString():(index).toString()));
          }
        }else{
          var res1 = formulaOne(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index+1).toDouble()));
          var res2 = formulaOne2(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index+1).toDouble()),ddVal: ddVal);
          widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString()));
          widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString()));
        }
        break;
      case GroupType.GroupTwo:
        if(dVal > 0){
          if (index == 0){
            var res1 = formulaTwo(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal,ddVal: ddVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "当前值",backgroundColor: Colors.redAccent,titleColor: Colors.white,width: 700));
          }else{
            var res1 = formulaTwo(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index).toDouble()),ddVal: ddVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index).toString(),width: 700,));
          }
        }else{
          var res1 = formulaTwo(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index+1).toDouble()),ddVal: ddVal);
          widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString(),width: 700,));
        }
        break;
      case GroupType.GroupThree:
        var res1 = formulaThree(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: ((index+1).toDouble()));
        widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString(),width: 700,backgroundColor: Colors.redAccent,titleColor: Colors.white));
        break;
      case GroupType.GroupFour:
        if(cVal == 0){ break; }
        var res1 = formulaFour(highVal: highVal,lowVal: lowVal,cVal: cVal);
        var res2 = formulaFour2(highVal: highVal,lowVal: lowVal,cVal: cVal);
        widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString(),backgroundColor: Colors.redAccent,titleColor: Colors.white));
        widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: dVal > 0?index.toString():(index+1).toString(),backgroundColor: Colors.blue,titleColor: Colors.white));
        break;
      case GroupType.GroupFive:
        if (highVal == 0||lowVal == 0) {break;}
        switch(index){
          case 0:
            var res = formulaFive1(highVal: highVal,lowVal: lowVal,cVal: cVal);
            widgets.add(ContentWidget(contentStr: res==0?'':res.toStringAsFixed(2), title: "上回",backgroundColor: Colors.redAccent,titleColor: Colors.white,width: 700,));
            break;
          case 1:
            var res1 = formulaFive21(highVal: highVal,lowVal: lowVal,cVal: cVal);
            var res2 = formulaFive22(highVal: highVal,lowVal: lowVal,cVal: cVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "上上1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
            widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "上上2",backgroundColor: Colors.blue,titleColor: Colors.white));
            break;
          case 2:
            var res1 = formulaFive31(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
            var res2 = formulaFive32(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "下反1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
            widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "下反2",backgroundColor: Colors.blue,titleColor: Colors.white));
            break;
          case 3:
            var res1 = formulaFive41(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
            var res2 = formulaFive42(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
            widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "下下1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
            widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "下下2",backgroundColor: Colors.blue,titleColor: Colors.white));
            break;
        }
        break;
      case GroupType.Group6:
        // TODO: Handle this case.
        var res1 = formulaSix1(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal).reversed.toList();
        var res2 = formulaSix2(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal).reversed.toList();
        widgets.add(ContentWidget(contentStr: res1[index]==0?'':res1[index].toStringAsFixed(2), title: lTitles[index],backgroundColor: Colors.redAccent,titleColor: Colors.white));
        widgets.add(ContentWidget(contentStr: res2[index]==0?'':res2[index].toStringAsFixed(2), title: rTitles[index],backgroundColor: Colors.blue,titleColor: Colors.white));
        break;
      case GroupType.Group7:
        // TODO: Handle this case.
         if (index == 0){
           var res1 = formula70(highVal: highVal,lowVal: lowVal,cVal: cVal);
           widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "上回",backgroundColor: Colors.redAccent,titleColor: Colors.white, width: 700));
         }else if (index == 1){
           var res1 = formula711(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           var res2 = formula712(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "上上1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
           widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "上上2",backgroundColor: Colors.blue,titleColor: Colors.white));
         }else if (index == 2){
           var res1 = formula721(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           var res2 = formula722(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "下反1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
           widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "下反2",backgroundColor: Colors.blue,titleColor: Colors.white));
         }else if (index == 3){
           var res1 = formula731(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           var res2 = formula732(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal: dVal);
           widgets.add(ContentWidget(contentStr: res1==0?'':res1.toStringAsFixed(2), title: "下下1",backgroundColor: Colors.redAccent,titleColor: Colors.white));
           widgets.add(ContentWidget(contentStr: res2==0?'':res2.toStringAsFixed(2), title: "下下2",backgroundColor: Colors.blue,titleColor: Colors.white));
         }
        break;
    }
    return widgets;
  }

  double formulaOne({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = tmp * sqrt(dVal);
    tmp = sqrt(tmp);
    tmp = tmp   + highVal;

    return tmp;
  }

  double formulaOne2({double highVal,double lowVal,double cVal, double dVal, double ddVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = tmp * sqrt(dVal);
    tmp = sqrt(tmp);
    tmp = tmp + ddVal;
    return tmp;
  }

  double formulaTwo({double highVal,double lowVal,double cVal, double dVal, double ddVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = tmp * sqrt(dVal);
    tmp = sqrt(tmp);
    tmp = tmp * highVal;
    tmp = sqrt(sqrt(tmp));
    tmp = tmp/2.4;
    tmp = tmp + ddVal;
    return tmp;
  }

  double formulaThree({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = sqrt(tmp);
    print(tmp);
    tmp = tmp / 1.7;
    print(tmp);
    tmp = tmp + highVal;
    return tmp;
  }

  double formulaFour({double highVal,double lowVal,double cVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = tmp / 2;
    tmp = tmp + highVal;
    return tmp;
  }

  double formulaFour2({double highVal,double lowVal,double cVal}){
    var tmp = highVal - lowVal;
    tmp = tmp / sqrt(cVal);
    tmp = sqrt(tmp);
    tmp = tmp + highVal;
    return tmp;
  }

  double formulaFive1({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal * lowVal;
    print(tmp);
    tmp = sqrt(tmp);
    print(tmp);
    return tmp;
  }

  double formulaFive21({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(sqrt(tmp));
    tmp = tmp * highVal;
    return tmp;
  }

  double formulaFive22({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = tmp * highVal;
    return tmp;
  }

  double formulaFive31({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = tmp * cVal;
    return tmp;
  }
  double formulaFive32({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = tmp * dVal;
    return tmp;
  }

  double formulaFive41({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(sqrt(tmp));
    tmp = lowVal / tmp;
    return tmp;
  }

  double formulaFive42({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = lowVal / tmp;
    return tmp;
  }

  List<double> formulaSix1({double highVal,double lowVal,double cVal, double dVal}){
    List<double> res = [];
    for(int i = 0; i < 6 ; i++){
      double tmp = 0;
      if (i == 0){
        tmp = highVal * (dVal + 1);
      }else{
        tmp = res[i-1] * (dVal + 1);
      }
      res.add(tmp);
    }
    return res;
  }

  List<double> formulaSix2({double highVal,double lowVal,double cVal, double dVal}){
    List<double> res = [];
    for(int i = 0; i < 6 ; i++){
      double tmp = 0;
      if (i == 0){
        tmp = highVal / (dVal + 1);
      }else{
        tmp = res[i-1] / (dVal + 1);
      }
      res.add(tmp);
    }
    return res;
  }

  double formula70({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp * 0.68;
    tmp = tmp + lowVal;
    return tmp;
  }
  double formula711({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(sqrt(tmp));
    tmp = tmp * highVal;
    return tmp;
  }
  double formula712({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp * (2-(log((highVal - lowVal))*1.618/10));
    tmp = tmp + lowVal;
    return tmp;
  }
  double formula721({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp * 0.5;
    tmp = highVal - tmp;
    return tmp;
  }

  double formula722({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = tmp * formula721(highVal: highVal,lowVal: lowVal,cVal: cVal,dVal:dVal);
    return tmp;
  }
  double formula731({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal - lowVal;
    tmp = tmp * 1.32;
    tmp = highVal - tmp;
    return tmp;
  }

  double formula732({double highVal,double lowVal,double cVal, double dVal}){
    var tmp = highVal / lowVal;
    tmp = sqrt(tmp);
    tmp = lowVal / tmp;
    tmp = tmp - 3;
    return tmp;
  }

  bool isNumeric(String str) {
    if (str.length == 0){return false;}
    if (str == '-'){return false;}
    try{
      var value = double.parse(str);
    } on FormatException {
      return false;
    } finally {
      return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, InputControllers4 controllers4, Widget widget) {
      double high = isNumeric(controllers4.controllerA.text) ?double.parse(controllers4.controllerA.text):0;
      double low = isNumeric(controllers4.controllerB.text) ?double.parse(controllers4.controllerB.text):0;
      double cVal = isNumeric(controllers4.controllerC.text) ?double.parse(controllers4.controllerC.text):0;
      double dVal = isNumeric(controllers4.controllerD.text) ?double.parse(controllers4.controllerD.text):0;
      double ddVal = isNumeric(controllers4.controllerDD.text) ?double.parse(controllers4.controllerDD.text):0;
      var count = 0;
      if(this.widget.type == GroupType.GroupThree ||this.widget.type == GroupType.GroupFour){
        count = 1;
      }else if (this.widget.type == GroupType.GroupFive ){
        count = 4;
      }else if (this.widget.type == GroupType.Group6){
        count = 6;
      }else if (this.widget.type == GroupType.Group7){
      count = 4;
      listHight = 280;
    }else{
        if(high > 0){
          count = 210;
        }
      }
      return Column(
        children: [
          Container(
            height: listHight.h,
            width: 720.w,
            margin: EdgeInsets.only(top: 10.h),
            child: ListView.builder(
              padding: EdgeInsets.all(0),
                itemCount: count,
                controller: _controller,
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  List<Widget> res = computeResultByType(lowVal: low,highVal: high,cVal: cVal,dVal: dVal,index: index,ddVal: ddVal);
                  return Container(
                    margin: EdgeInsets.only(bottom: 10.h),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: res,
                    ),
                  );
                }),
          ),
          count > 10 ?
          Container(
            height: 84.h,
            margin: EdgeInsets.only(top: 20.h),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                MaterialButton(
                  child: Container(
                    width: 300.w,
                    height: 84.h,
                    alignment: Alignment.center,
                    child: Text("上一页",style: TextStyle(color: Colors.white)),
                    decoration: BoxDecoration(
                      color: Colors.lightBlue,
                      borderRadius: BorderRadius.all(Radius.circular(8)),
                        boxShadow: [BoxShadow(color: Colors.black45,offset: Offset(0, 0),blurRadius: 4.0,)]
                    ),
                  ),
                  onPressed: (){
                    if (page.value != 0 ){
                      page.value--;
                      //_controller.animateTo(listHight.h * page, duration: Duration(microseconds: 300), curve: Curves.ease);
                    }else{
                      BotToast.showText(text: "已到首页");
                    }
                  },
                ),
                MaterialButton(
                  child: Container(
                    width: 300.w,
                    height: 84.h,
                    alignment: Alignment.center,
                    child: Text("下一页",style: TextStyle(color: Colors.white)),
                          decoration: BoxDecoration(
                          color: Colors.lightBlue,
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                              boxShadow: [BoxShadow(color: Colors.black45,offset: Offset(0, 0),blurRadius: 4.0,)]
                    ),
                  ),
                  onPressed: (){
                    if(page.value < 20){
                      page.value++;
                     // _controller.animateTo(listHight.h * page, duration: Duration(microseconds: 300), curve: Curves.ease);
                    }else{
                      BotToast.showText(text: "已到最后一页");
                    }
                  },
                )
              ],
            ),
          ):
          Container()
        ],
      );
    });
  }
}

class ContentWidget extends StatelessWidget {
  final String title;
  final String contentStr;
  final Color backgroundColor;
  final Color titleColor;
  final double width;

  ContentWidget({
    @required this.title,
    @required this.contentStr,
    this.backgroundColor = Colors.white,
    this.titleColor=Colors.black,
    this.width=340
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width.w,
      height: 50.h,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.all(Radius.circular(8)),
          boxShadow: [BoxShadow(color: Colors.black45,offset: Offset(0, 0),blurRadius: 1.h)]
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            child: Text(title,style: TextStyle(color: this.titleColor,fontWeight: FontWeight.w700)),
            margin: EdgeInsets.fromLTRB(20.w, 0, 0, 0),
            padding: EdgeInsets.only(right: 20.w),
            decoration: BoxDecoration(
              border: Border(right: BorderSide(color: this.titleColor))
            ),
          ),
          Expanded(child: Text(contentStr,style: TextStyle(color: this.titleColor),textAlign: TextAlign.center))
        ],
      ),
    );
  }
}